package ift604.projet.feeling;

import ift604.projet.R;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.location.Location;
import android.widget.Toast;

public class FeelingWithPosition {
 
    public static boolean associateFeelingWithGeoPosition(Activity activity, Feeling feeling, Location location) {
    	if (location == null) {
    		Toast.makeText(activity.getBaseContext(), activity.getResources().getString(R.string.geoloc_not_working), Toast.LENGTH_LONG).show();
    		return false;
    	}
    	
    	boolean result = true;
    	
    	HttpClient httpclient = new DefaultHttpClient();
    	HttpPut myPutRequest = new HttpPut("http://ift604.alixg.com/api/feeling");
    	
    	try {
    		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("feeling_name", feeling.getName()));
            nameValuePairs.add(new BasicNameValuePair("latitude", ""+location.getLatitude()));
            nameValuePairs.add(new BasicNameValuePair("longitude", ""+location.getLongitude()));
            myPutRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            httpclient.execute(myPutRequest);
            
    	} catch (ClientProtocolException e) { 
    		result = false;
        } catch (IOException e) {
        	result = false;
        }
    	
    	return result;
    }
}
